/*******************************************************************************
 * Copyright (c) 2022, 2022
 ******************************************************************************/
package com.alex.demo.swagger.config;

import java.util.ArrayList;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.env.Environment;
import org.springframework.core.env.Profiles;

import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.service.Contact;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;

/**
 * @Version 1.0
 * @Author Alex
 * @Created Dec 2022/9/30 11:11
 * @Description
 *              <p>
 */
@Configuration
@EnableSwagger2 // 开启swagge2
public class SwaggerConfig {

	// 配置Swagger 的 Docket实例
	@Bean
	public Docket docket(Environment environment) {
		Profiles profiles = Profiles.of("dev", "test");// 只在 dev 和 test 环境下，才开启Swagger
		// 通过environment.acceptsProfiles判断是否处在自己设定的环境中
		boolean flag = environment.acceptsProfiles(profiles);

		return new Docket(DocumentationType.SWAGGER_2).groupName("Alex").apiInfo(apiInfo())
				// enable：是否启动swagger,若为false，则Swagger不能在浏览器访问
				.enable(flag).select()
				// RequestHandlerSelectors ，配置要扫描接口的方式
				// basePackage：指定要扫描的包
				// any()：扫描全部
				// none()：不扫描
				// withClassAnnotation：扫描类上的注解，参数是一个注解的反射对象
				// withMethodAnnotation：扫描方法上的注解
				.apis(RequestHandlerSelectors.basePackage("com.alex.demo.swagger.controller"))

				// paths()：指定要过滤的路径
				// .paths(PathSelectors.ant("/alex/**"))
				.build();
	}

	@Bean
	public Docket docketA() {
		return new Docket(DocumentationType.SWAGGER_2).groupName("Group_A");
	}

	@Bean
	public Docket docketB() {
		return new Docket(DocumentationType.SWAGGER_2).groupName("Group_B");
	}

	private ApiInfo apiInfo() {
		Contact contact = new Contact("Alex", "https://gitee.com/Alex_taotao", "12345@qq.com");
		return new ApiInfo("Ale的Swagger-API文档", "再小的帆也能远航", "1.0", "https://gitee.com/Alex_taotao", contact, "Apache 2.0",
				"http://www.apache.org/licenses/LICENSE-2.0", new ArrayList<>());
	}

}
